package com.lifeng.patterns.strategy1;

import com.lifeng.patterns.strategy.PayType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lifeng
 * @version v1.0
 * @date 2020/12/12 15:28
 * @description 策略模式
 */
@Component
public class PayFactory implements InitializingBean {
    @Resource
    private List<PayType> payTypeList;

    private Map<String,PayType> payTypeMap = new HashMap<>();

	public PayType getPayService(String payType){
		return payTypeMap.get(payType);
	}
    
    @Override
    public void afterPropertiesSet() throws Exception {
        payTypeMap = payTypeList.stream().collect(Collectors.toMap(x->x.getPayType(),y->y));
    }
}
